﻿using System;
using RestSharp;
using Web.Core.EventArgs;
using Web.Core.Models.Game;
using Ayatta.Contract.Domain;

namespace Web.Core.GameProxys
{
    internal abstract class GameClient : IGameClient
    {
        protected const int Timeout = 5000;
        protected readonly IRestClient Client;

        public int GameId { get; private set; }
        public string BaseUrl { get; protected set; }
        public string DefaultKey { get; protected set; }

        public event EventHandler<EventArgs<UserInfo>> Updated;
        public event EventHandler<EventArgs<OrderInfo>> Recharged;

        protected GameClient(int gameId)
        {
            GameId = gameId;
            Client = new RestClient();
        }

        protected virtual void OnUpdated(EventArgs<UserInfo> e)
        {
            var handler = Updated;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnRecharged(EventArgs<OrderInfo> e)
        {
            var handler = Recharged;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        /// <summary>
        /// 获取登录url
        /// </summary>
        /// <param name="guid">用户GUID</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public abstract string GetLoginUri(string guid, params string[] param);

        /// <summary>
        /// 检查用户是否创建角色
        /// </summary>
        /// <param name="guid">用户GUID</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public abstract MagicKit<string> CheckUser(string guid, params string[] param);

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="guid">用户GUID</param>
        /// <param name="serverId">游戏区服Id</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public abstract MagicKit<UserInfo> GetUser(string guid, int serverId, params string[] param);

        /// <summary>
        /// 充值
        /// </summary>
        /// <param name="guid">用户GUID</param>
        /// <param name="order">订单</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public abstract MagicKit<OrderInfo> Recharge(string guid, OrderInfo order, params string[] param);

       protected static string GetUnixTime()
        {
            var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));

            var nowTime = DateTime.Now;

            var unixTime = (long)Math.Round((nowTime - startTime).TotalSeconds, MidpointRounding.AwayFromZero);
            return unixTime.ToString();
        }
    }
}